<?php

/**
 * User Image Creation
 * 
 * @project : Loobica Ads
 * @author  : Dino
 * @date    : 1 Apr 2014
 * 
 */
class User_image {

  var $width = 1024;
  var $height = 768;
  var $quality = 50;

  public function __construct() {
    $this->ci = & get_instance();
  }

  /**
   * Set Image Size
   * 
   * @param type $width
   * @param type $height
   * @return \User_image
   */
  public function size($width = 0, $height = 0) {
    $this->width = (int) $width;
    $this->height = (int) $height;
    return $this;
  }

  /**
   * Make New JPEG Image
   * 
   * @param type $file
   * @param type $new_file
   * @return null
   */
  public function generate($image_file = null, $new_img_file = null, $new_tmb_file = null, $square = false) {

    // Determine image type
    $image_file_details = getimagesize($image_file);

    // Creating image resource
    switch ($image_file_details['mime']) {
      case "image/gif":
        $img_resrc = imagecreatefromgif($image_file);
        break;
      case "image/jpeg":
        $img_resrc = imagecreatefromjpeg($image_file);
        break;
      case "image/png":
        $img_resrc = imagecreatefrompng($image_file);
        break;
      default :
        $img_resrc = null;
    }

    // Checkinig img
    if (!empty($img_resrc)) {

      // Getting original image size
      $orig_width = ImageSX($img_resrc);
      $orig_height = ImageSY($img_resrc);

      // Original Image reduce size
      $im_fact = min(($this->height / $orig_height), ($this->width / $orig_width));
      $orig_width_new = round($orig_width * $im_fact);
      $orig_height_new = round($orig_height * $im_fact);

      // Square crop image for thumbnail
      if ($orig_width > $orig_height) {
        $new_width = $orig_height;
        $new_height = $orig_height;
        $x = round(($orig_width / 2) - ($new_width / 2));
        $y = 0;
      } elseif ($orig_height > $orig_width) {
        $new_width = $orig_width;
        $new_height = $orig_width;
        $x = 0;
        $y = round(($orig_height / 2) - ($new_height / 2));
      } else {
        $new_width = $orig_width;
        $new_height = $orig_height;
        $x = 0;
        $y = 0;
      }

      // If Squre Image Needed
      if ($square) {
        $square_size = (($this->width + $this->height) / 2);
        $orig_width_new = $orig_height_new = $square_size;
        $orig_width = $new_width;
        $orig_height = $new_height;
      }

      // Image in 640x480 size (Keeping aspect ratio)
      $new_img_resrc = imagecreatetruecolor($orig_width_new, $orig_height_new);
      $white = imagecolorallocate($new_img_resrc, 255, 255, 255);
      imagefill($new_img_resrc, 0, 0, $white);
      imageinterlace($new_img_resrc, TRUE);
      imagecopyresampled($new_img_resrc, $img_resrc, 0, 0, 0, 0, $orig_width_new, $orig_height_new, $orig_width, $orig_height);
      imagejpeg($new_img_resrc, $new_img_file, $this->quality);
      imagedestroy($new_img_resrc);

      // Image square thumbnail in 200x200 size
      if (!empty($new_tmb_file)) {
        $new_img_resrc = imagecreatetruecolor(200, 200);
        $white = imagecolorallocate($new_img_resrc, 255, 255, 255);
        imagefill($new_img_resrc, 0, 0, $white);
        imageinterlace($new_img_resrc, TRUE);
        imagecopyresampled($new_img_resrc, $img_resrc, 0, 0, $x, $y, 200, 200, $new_width, $new_height);
        imagejpeg($new_img_resrc, $new_tmb_file, $this->quality);
        imagedestroy($new_img_resrc);
      }

      // Clear cache
      imagedestroy($img_resrc);

      // Checking weather new images created & return result
      if (file_exists($new_img_file)) {
        return true;
      }
    }

    return false;
  }

}

?>